package controllers;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import models.json.GoogleImageResult;
import play.libs.WS;
import play.mvc.Controller;

import java.io.IOException;

public class InternetImage extends Controller {
    public static void getImages(String title, int start) throws IOException {
        GoogleImageResult googleImageResult = getGoogleImages(title, start);
        render(googleImageResult);
    }
    private static GoogleImageResult getGoogleImages(String title, int start) throws IOException {
        String url = String.format("https://ajax.googleapis.com/ajax/services/search/images?start=%2$s&rsz=8&v=1.0&q=%1$s logo", title, start);
        WS.HttpResponse httpResponse = WS.url(url.replace(" ", "+")).get();
        JsonElement json = httpResponse.getJson();
        Gson gson = new Gson();
        GoogleImageResult googleImage = gson.fromJson(((JsonObject) json).get("responseData"), GoogleImageResult.class);
        return googleImage;
    }
}
